package com.example.itcacp.controller;

import com.example.itcacp.dto.response.Result;
import com.example.itcacp.entity.Comment;
import com.example.itcacp.entity.Like;
import com.example.itcacp.service.CommentService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/api/comment")
public class CommentController {

    @Resource
    private CommentService commentService;


    /**
     * 查询对应内容评论列表
     * */
    @GetMapping("/commentList/{contentType}/{contentId}")
    public Result commentList (@PathVariable Like.ContentType contentType,
                               @PathVariable Long contentId) {
        List<Comment> commentList = commentService.commentList ( contentId, contentType );
        return Result.success (commentList);
    }

    /**
     * 发布评论
     * */
    @PostMapping("/addComment")
    public Result addComment (@RequestBody Comment comment) {
        comment.setCreated_at ( new Timestamp ( System.currentTimeMillis () ).toLocalDateTime () );  // 添加时间
        commentService.addComment(comment); // 添加评论
        Result newCommentList = commentList ( comment.getTargetType (), comment.getTargetId () ); //  更新评论列表
        return Result.success (newCommentList);
    }
}
